// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import Vuex from 'vuex'
import '@/assets/css/app.css'
import filterActive from '@/tools/methods.js'

Vue.use(Vuex);

let data = JSON.parse(localStorage.getItem('item-value')) || [];
let store = new Vuex.Store({
  state: {
    inputValue:'',
    list:data,
    hash:'all'
  },
  mutations:{
    enter(state,parmas){
      state.list.unshift({
        title:parmas.title,
        id: parmas.id,
        checked: parmas.checked
      });
    },
    remove(state,index){
      state.list.splice(index,1);
    },
    save(state,item){
      let itemIndex = item.id;
      state.list.splice(itemIndex,1,item.item);
    },
    updata(state,item){
      state.list = item;
    },
    setItem(state,par){
      state.list.forEach(item => item.checked = par);
    },
    setHash(state,par){
      state.hash = par;
    }
  },
  actions:{

  }
})

Vue.config.productionTip = false
/* eslint-disable no-new */
let vm = new Vue({
  el: '#app',
  store,
  components: { App },
  template: '<App/>',
  watch:{
    comList :{
      handler (){
        localStorage.setItem('item-value',JSON.stringify(this.$store.state.list));
      },
      deep:true,
    }
  },
  computed:{
    comList(){
      return this.$store.state.list
    }
  }
})

window.onhashchange = function (){
  let hashVal = window.location.hash;
  if(hashVal){
      hashVal = hashVal.slice(2);
      hashVal = filterActive[hashVal] ? hashVal : 'all'
  }else{
      hashVal = 'all';
  }
  // 改变实例下的过滤条件
  vm.$store.commit("setHash",hashVal);
}
window.onhashchange();
